package com.three.controller;

import com.github.benmanes.caffeine.cache.Cache;
import com.three.mapper.CourseMapper;
import com.three.pojo.CourseInfo;
import com.three.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/course")
public class Course {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private Cache<String, CourseInfo> courseCache;

    @GetMapping
    public Result getCourse(int classId, int weekNum) {
        log.info("日志 GET /course 班级id:" + classId + "周数:" + weekNum);
        CourseInfo result = courseCache.getIfPresent(classId + ":" + weekNum);
        if (result == null) {
            // 如果缓存没有储存, 则往数据库获取数据
            result = courseMapper.select(classId, weekNum);
            courseCache.put(classId + ":" + weekNum, result);
        }
        return Result.success("课表获取成功", result);
    }
}
